package gui.components.JXTable.JXColumn;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.HashMap;
import java.util.Vector;

/**
 * Date: 26.09.2005
 * Time: 18:54:39
 */
public class JXColumnHeaderRenderer extends JList implements TableCellRenderer{

    private HashMap<Integer, recordParametrs> params = new HashMap<Integer, recordParametrs>();
    private recordParametrs tParam = new recordParametrs();
    private Icon defIcon = new ImageIcon();
    private int defAlign = JLabel.CENTER;
    private Color defBackColor = null;
    private labelCellRenderer renderer = new labelCellRenderer();
    private TableCellRenderer tableCellRenderer;

    public JXColumnHeaderRenderer(TableCellRenderer tableCellRenderer) {
        this.tableCellRenderer = tableCellRenderer;
    }



    public JXColumnHeaderRenderer() {
        setOpaque(true);
        setForeground(UIManager.getColor("TableHeader.foreground"));
        setBackground(UIManager.getColor("TableHeader.background"));
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));

        renderer.setHorizontalAlignment(JLabel.CENTER);
        setCellRenderer(renderer);

        this.defBackColor = UIManager.getColor("TableHeader.background");
    }

    public void setIcon(int column,Icon icon) {
        if (params.containsKey(column)){
            params.get(column).setIcon(icon);
        }else{
            recordParametrs param = new recordParametrs();
            param.setIcon(icon);
            params.put(column,param);
        }
    }

    public void setAlign(int column,int align) {
        if (params.containsKey(column)){
            params.get(column).setAlign(align);
        }else{
            recordParametrs param = new recordParametrs();
            param.setAlign(align);
            params.put(column,param);
        }
    }

    public void setBackground(int column,Color color) {
        if (params.containsKey(column)){
            params.get(column).setBackColor(color);
        }else{
            recordParametrs param = new recordParametrs();
            param.setBackColor(color);
            params.put(column,param);
        }
    }

    public void removeColumnParams(int column){
        if(params.containsKey(column)){
            params.remove(column);
        }
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setFont(table.getFont());
        this.tParam = (params.containsKey(column))?params.get(column):null;
        ((JLabel)this.renderer).setHorizontalAlignment((tParam!=null)?tParam.getAlign():defAlign);
        ((JLabel)this.renderer).setIcon((tParam==null)?defIcon:(tParam.getIcon()==null)?defIcon:tParam.getIcon());
        this.setBackground((tParam==null)?defBackColor:(tParam.getBackColor()==null)?defBackColor:tParam.getBackColor());
        String str = (value == null) ? "" : value.toString();
        String s[] = str.split("\\;");
        Vector v = new Vector();
        for(int i=0;i<s.length;i++){v.addElement(s[i]);}
        setListData(v);

        return this;
    }

    private class labelCellRenderer extends JLabel implements ListCellRenderer{
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            setText((value!=null)?value.toString():"");
            return this;
        }
    }

    private class recordParametrs extends Object{
        private Icon icon = null;
        private int align = JLabel.CENTER;
        private Color backColor = null;
        public void setIcon(Icon icon){this.icon = icon;}
        public void setAlign(int align){this.align = align;}
        public void setBackColor(Color color){this.backColor = color;}
        public Icon getIcon(){return this.icon;}
        public int getAlign(){return align;}
        public Color getBackColor(){return backColor;}
    }

}
